{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "import os\n",
    "import cv2\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.jpg 1\n",
      "\n",
      "2.jpg 1\n",
      "\n",
      "3.jpg 1\n",
      "\n",
      "4.jpg 0\n",
      "\n",
      "5.jpg 0\n",
      "\n",
      "6.jpg 0\n",
      "\n",
      "7.jpg 1\n",
      "\n",
      "8.jpg 0\n",
      "\n",
      "9.jpg 0\n",
      "\n",
      "10.jpg 1\n",
      "\n",
      "11.jpg 1\n",
      "\n",
      "12.jpg 0\n",
      "\n",
      "13.jpg 1\n",
      "\n",
      "OVER\n"
     ]
    }
   ],
   "source": [
    "imgPath = 'C:/Users/q/Desktop/Code/Python/demo/resize/melonjson/test/'\n",
    "\n",
    "jsonPath = 'C:/Users/q/Desktop/Code/Python/demo/resize/melonjson/test/outputs/'\n",
    "out_path = 'C:/Users/q/Desktop/Code/Python/demo/resize/melonjson/test32/'\n",
    "\n",
    "# f = open(txt, 'r')  # 以只读形式打开txt文件\n",
    "# contents = f.readlines()  # 读取文件中所有行\n",
    "# f.close()  # 关闭txt文件\n",
    "# x, y_ = [], []  # 建立空列表\n",
    "# for content in contents:  # 逐行取出\n",
    "#     value = content.split()  # 以空格分开，图片路径为value[0] , 标签为value[1] , 存入列表\n",
    "#     img_path = path + value[0]  # 拼出图片路径和文件名\n",
    "#     img = Image.open(img_path)  # 读入图片\n",
    "#     img2=img.resize((32,32))\n",
    "#     img2.save(out_path+value[0])\n",
    "#     print('loading : ' + content)  # 打印状态提示\n",
    "\n",
    "############################read_json################################\n",
    "\n",
    "for i in range(1,14):\n",
    "    n = (str)(i)\n",
    "    jsonPathn = jsonPath+n+'.json'\n",
    "    file = open(jsonPathn, \"rb\")\n",
    "    data = json.load(file)\n",
    "    \n",
    "    outputs = data['outputs']\n",
    "    name = outputs.get(\"object\")\n",
    "    lable = name[0].get(\"name\")\n",
    "    fh = open('C:/Users/q/Desktop/Code/Python/demo/resize/melonjson/test.txt', 'a', encoding='utf-8')\n",
    "    fh.write(n+'.jpg'+\" \"+lable+\"\\n\")\n",
    "    print(n+'.jpg'+\" \"+lable+\"\\n\")\n",
    "    fh.close()\n",
    "#     print(jsonPathn)\n",
    "    \n",
    "#############################resize_img#################################\n",
    "\n",
    "# for i in range(1,14):\n",
    "#     n = (str)(i)\n",
    "#     imgPathn = imgPath+n+'.jpg'\n",
    "#     img = Image.open(imgPathn)  # 读入图片\n",
    "#     img2=img.resize((32,32))\n",
    "#     img2.save(out_path+n+'.jpg')\n",
    "#     print('loading : ' + imgPathn)  # 打印状态提示\n",
    "\n",
    "###############################################################\n",
    "\n",
    "print(\"OVER\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
